package com.aspyn.pong;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

public class DrawView extends View implements OnTouchListener {

	private Ball ball1;
	private Paddle paddle1;
	private Paint paint;
	private Paint paint2;

	private TextView txtScore;

	final int kBallRadius = 16;
	final int kBallStartX = 150;
	final int kBallStartY = 150;

	private double paddleVelocity = 0;
	private int paddleMoved = 0;//0 is no, 1 is left, 2 is right
	final int kPaddleStartX = 240;//paddle Center x
	final int kPaddleStartY = 585;//paddle Center y
	final int kPaddleHeight = 20;
	final int kPaddleWidth = 113;
	final int kPaddleCornerRadius = 10;

	final double kMultiplierBallVel = 0.20;

	private int kScoreVelDecMultiple = 5;
	private int kDefaultMaxBallVel = 30;
	final int kSpeedIncreaseConstant = 2;
	private int kDefaultBallVel = 7;
	private int kOriginalBallVel = 7;
	private int score = 0;

	private int screenWidth = 480;
	private int screenHeight = 720;

	int gameState = 1;//0 is paused, 1 is playing, 2 is TBD

	public DrawView(Context context, TextView tv) {
		super(context);
		setFocusable(true);

		//	screenHeight = this.getHeight();
		//screenWidth = this.getWidth();

		ball1 = new Ball(kBallStartX, kBallStartY, kDefaultBallVel, kDefaultBallVel, kBallRadius, Color.BLUE, 0);
		paddle1 = new Paddle(kPaddleStartX,kPaddleStartY,kPaddleWidth,kPaddleHeight,Color.BLUE);

		setFocusableInTouchMode(true);

		this.setOnTouchListener(this);

		txtScore = tv;
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onDraw(Canvas c) {
		if (gameState == 1) {
			double x = ball1.getX(), y = ball1.getY();
			int r = ball1.getRadius();

			if (x+r >= screenWidth || x-r <= 0) {
				ball1.setXVel(-ball1.getXVel());
			}
			if (y+r >= screenHeight || y-r <= 0) {
				ball1.setYVel(-ball1.getYVel());
			}
			System.out.println("playing");
			ball1.move(ball1.getX()+ball1.getXVel(), ball1.getY()+ball1.getYVel(), ball1);
		}
		paint = new Paint();
		paint.setColor(ball1.getColor());
		paint.setAntiAlias(true);

		paint2 = new Paint();
		paint2.setColor(Color.CYAN);//Change to paddle color
		paint2.setAntiAlias(true);

		c.drawCircle((float)ball1.getX(), (float)ball1.getY(), ball1.getRadius(), paint);//Ball class manages the circle (its coordinates, color, velocity, etc.)

		RectF rect = new RectF(paddle1.getX()-(kPaddleWidth/2),kPaddleStartY-(kPaddleHeight/2),paddle1.getX()+(kPaddleWidth/2),kPaddleStartY+(kPaddleHeight/2));
		c.drawRoundRect(rect, kPaddleCornerRadius, kPaddleCornerRadius, paint2);

		int radius = ball1.getRadius();

		//Check for game end after ball moves
		if (ball1.getY()+radius >= screenHeight ) {
			endGame();
			invalidate();
			return;
		}

		RectF ballRect = new RectF((float)ball1.getX()-radius, (float)ball1.getY()-radius, (float)ball1.getX()+radius, (float)ball1.getY()+radius);

		if (rect.intersect(ballRect) && ball1.getY()+radius<(paddle1.getY()-kPaddleHeight/2)+5 && gameState == 1) {//Don't say collision if ball is lower than paddle, and game must be in playing state to enter this if

			//Ball intersects paddle-divide paddle into thirds for deflection
			double x1 = paddle1.getX()-(kPaddleWidth/2)+(.25*kPaddleWidth);
			double x2 = paddle1.getX()-(kPaddleWidth/2)+(.75*kPaddleWidth);
			//p1.set((int) (paddle1.getX()-(kPaddleWidth/2)+(.33*kPaddleWidth)), kPaddleStartY);//___p1___p2___
			//p2.set((int) (paddle1.getX()-(kPaddleWidth/2)+(.66*kPaddleWidth)), kPaddleStartY);
			double ballX = ball1.getX();
			double v = ball1.getXVel();

			//Add 1 to score if ball hits TOP of paddle
			if (ball1.getY()<paddle1.getY()) {
				score++;
				//update score label
				updateScore();
			}
			else {//End game because ball shouldn't paddle BOTTOM
				endGame();
				invalidate();
			}

			if (!(paddleVelocity < 1 && paddleVelocity > -1)) {
				//Ball moves at same speed
				System.out.println(paddleVelocity);
				ball1.setXVel(kDefaultBallVel*(paddleVelocity*.08));//Ball speed changes based off paddle
			}
			/*if (v < 0) {//Ball moving left
				if (ballX > x2) {//p2_X_slowest
					ball1.setXVel((int)-kDefaultBallVel*(kMultiplierBallVel*2));
				}
				else if (ballX >= x1) {//p1_X_p2___normal
					ball1.setXVel(-kDefaultBallVel);
				}
				else {//_X_p1___p2___fastest
					ball1.setXVel((int)-(kDefaultBallVel+kDefaultBallVel*kMultiplierBallVel));
				}
			}
			else {//Ball moving right
				if (ballX > x2) {//p2_X_fastest
					ball1.setXVel((int)kDefaultBallVel+kDefaultBallVel*kMultiplierBallVel);
				}
				else if (ballX >= x1) {//p1_X_p2___normal
					ball1.setXVel(kDefaultBallVel);
				}
				else {//_X_p1___p2___slowest
					ball1.setXVel(kDefaultBallVel*(kMultiplierBallVel*2));
				}
			}*/


			ball1.setYVel(-ball1.getYVel());
			paddleMoved = 0;
			paddleVelocity = 0;
		}


		invalidate();
	}

	//End Game/New Game
	public void endGame() {
		//Display score somehow - Toast for now
		Toast.makeText(getContext(), "Your final score was " + Integer.toString(score), 1).show();
		//update score label
		score = 0;
		updateScore();
		kDefaultBallVel = kOriginalBallVel;

		ball1.setXVel(kDefaultBallVel);
		ball1.setX(kBallStartX);
		ball1.setY(kBallStartY);
	}

	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if (event.getX()<paddle1.getX()) {
			//moved left
			paddleMoved = 1;
		}
		else if (event.getX()>paddle1.getX()){
			//moved right
			paddleMoved = 2;
		}
		paddleVelocity = event.getX()-paddle1.getX();
		paddle1.setX((int)event.getX());
		//	ball1.setX((int)event.getX());MAKE PADDLE MOVE TO TOUCH
		//ball1.setY((int)event.getY());
		return true;
	}

	public void updateScore() {
		//Check for increase speed
		if (kDefaultBallVel<kDefaultMaxBallVel) {
			if (score % kScoreVelDecMultiple == 0) {
				kDefaultBallVel += kSpeedIncreaseConstant;
				Toast.makeText(getContext(), "Speed Increased", 1).show();
			}
		}

		txtScore.setText("Score: " + Integer.toString(score));
	}

	//menu stuff
	public void switchBallColor(int newColor) {
		gameState = 0;
		ball1.setColor(newColor);
		Toast.makeText(getContext(), "Color switch", 1).show();
	}

	public void setGameState(int gS) {
		gameState = gS;
	}
}